/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package me.haeger.assembler;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/**
 *
 * @author Häger
 */
public class Assembler {

    private static final String ENCODING = "UTF-8";
    private static final String LOGO = "                      o       Oo                       o     o       \n       .oOOo. .oOOo. O       o  O                     O     O        \n       O    o O    o O      O    o                    O     o        \n       o    O o    O o     oOooOoOo                   o     O        \n'OoOo. o    o o    o OoOo. o      O .oOo  .oOo  .oOo. OoOo. o  O   o \n o   O O    O O    O O   o O      o `Ooo. `Ooo. OooO' O   o O  o   O \n O   o o    O o    O o   O o      O     O     O O     o   O o  O   o \n o   O `OooO' `OooO' `OoO' O.     O `OoO' `OoO' `OoO' `OoO' Oo `OoOO \n                                                                   o \n  v1.0 - By: Haeger                                             OoO' \n";
            
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        System.out.println(LOGO);

        String text = null;
                
        try {
            text = read(args[0]);            
        } catch (FileNotFoundException ex) {
            //Logger.getLogger(Assembler.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println();
            System.err.println("File not found at: " + args[0]);            
            
        } catch (IOException ex) {
            //Logger.getLogger(Assembler.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println();
            System.err.println("Error while attempting to read file at: " + args[0]);
        }        
        
        Encoder binary = new Encoder(text);
        binary.assemble();
        write(binary.print());
        //System.out.println(binary.print());
    }    
    
    private static String read(String filePath) throws IOException {
        System.out.print("Attempting to read file... ");
        Scanner scanner = new Scanner(new FileInputStream(filePath), ENCODING);
        String NL = System.getProperty("line.separator");
        StringBuilder text = new StringBuilder();
        
        try {            
            while(scanner.hasNextLine()) {
                text.append(scanner.nextLine()).append(NL);
            }
        }
        finally {
            scanner.close();
        }
        
        System.out.println("Done.");
        return text.toString();
    }
    
    private static void write(String code) {
        System.out.print("Attempting to write file... ");
        try {
            BufferedWriter file = new BufferedWriter(new FileWriter("assemblatOklart.txt"));
            file.write(code);
            file.close();
        } catch (IOException ex) {
            System.err.println("Error while writing file.");
        }
        System.out.println("Done.");
    }
    
}
